// Copyright (c) 2022 NetEase, Inc. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.

package com.netease.yunxin.app.im.message;

import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.v2.message.V2NIMMessage;
import com.netease.nimlib.sdk.v2.message.V2NIMMessageCreator;
import com.netease.nimlib.sdk.v2.utils.V2NIMConversationIdUtil;
import com.netease.yunxin.kit.alog.ALog;
import com.netease.yunxin.kit.chatkit.repo.ChatRepo;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.netease.yunxin.kit.chatkit.model.IMMessageInfo;
import com.netease.yunxin.kit.chatkit.ui.ChatKitClient;

/**
 * 商品消息工具类
 */
public class ProductMessageUtils {
    
    private static final String TAG = "ProductMessageUtils";
    
    /**
     * 发送商品消息
     */
    public static void sendProductMessage(String targetId, String productId, String productName, 
                                        String productPrice, String productImage, String productDesc, 
                                        String productUrl) {
        ALog.d(TAG, "=== 开始发送商品消息 ===");
        ALog.d(TAG, "targetId: " + targetId);
        ALog.d(TAG, "productId: " + productId);
        ALog.d(TAG, "productName: " + productName);
        ALog.d(TAG, "productPrice: " + productPrice);
        ALog.d(TAG, "productImage: " + productImage);
        ALog.d(TAG, "productDesc: " + productDesc);
        ALog.d(TAG, "productUrl: " + productUrl);
        
        try {
            // 1. 创建 ProductAttachment
            ALog.d(TAG, "创建 ProductAttachment...");
            ProductAttachment attachment = new ProductAttachment(
                productId, productName, productPrice, productImage, productDesc, productUrl
            );
            ALog.d(TAG, "ProductAttachment created successfully");
            ALog.d(TAG, "ProductAttachment type: " + attachment.getType());
            ALog.d(TAG, "ProductAttachment JSON: " + attachment.toJsonStr());
            
            // 2. 用 V2NIMMessageCreator 创建自定义消息
            ALog.d(TAG, "用 V2NIMMessageCreator 创建自定义消息...");
            V2NIMMessage message = V2NIMMessageCreator.createCustomMessage(
                attachment.toJsonStr(), // 这里传 JSON 字符串
                attachment.toJsonStr()
            );
            ALog.d(TAG, "V2NIMMessage created successfully");

            // 3. 发送消息
            ALog.d(TAG, "发送消息...");
            String conversationId = V2NIMConversationIdUtil.conversationId(targetId, SessionTypeEnum.Team);
            ChatRepo.sendMessage(message, conversationId, null, null);
            ALog.d(TAG, "=== 商品消息发送成功 ===");
            
        } catch (Exception e) {
            ALog.e(TAG, "Send product message failed: " + e.getMessage());
            ALog.e(TAG, "Exception stack trace:");
            e.printStackTrace();
        }
    }
    
    /**
     * 发送商品消息（简化版）
     */
    public static void sendProductMessage(String targetId, String productName, String productPrice, 
                                        String productImage, String productDesc) {
        ALog.d(TAG, "发送简化版商品消息");
        String productId = String.valueOf(System.currentTimeMillis());
        sendProductMessage(targetId, productId, productName, productPrice, productImage, productDesc, "");
    }
    
    /**
     * 发送测试商品消息
     */
    public static void sendTestProductMessage(String targetId) {
        ALog.d(TAG, "=== 发送测试商品消息 ===");
        ALog.d(TAG, "targetId: " + targetId);
        
        String productId = "test_product_001";
        String productName = "测试商品";
        String productPrice = "¥99.00";
        String productImage = "https://via.placeholder.com/200x200/FF0000/FFFFFF?text=Product";
        String productDesc = "这是一个测试商品，用于演示自定义商品消息功能";
        String productUrl = "https://www.example.com/product/001";
        
        ALog.d(TAG, "测试商品信息:");
        ALog.d(TAG, "- productId: " + productId);
        ALog.d(TAG, "- productName: " + productName);
        ALog.d(TAG, "- productPrice: " + productPrice);
        ALog.d(TAG, "- productImage: " + productImage);
        ALog.d(TAG, "- productDesc: " + productDesc);
        ALog.d(TAG, "- productUrl: " + productUrl);
        
        sendProductMessage(targetId, productId, productName, productPrice, productImage, productDesc, productUrl);
    }
}